#include "head.h"

#if 1
u32_t * rgb24to32(u8_t *buf24, fb_info jpeg_inf)
{
	int w = jpeg_inf.w;
	int h = jpeg_inf.h;
	
	u32_t *buf = malloc(w * h * 4);
	if (buf == NULL) return NULL;

	int i;
	for(i = 0; i < w * h; ++i){
		*((u8_t *)&buf[i] + 2) = buf24[i * 3 + 0];
		*((u8_t *)&buf[i] + 1) = buf24[i * 3 + 1];
		*((u8_t *)&buf[i] + 0) = buf24[i * 3 + 2];
		*((u8_t *)&buf[i] + 3) = 0x00;
	}
	return (u32_t *)buf;
}
#endif

#if 0
u16_t * rgb24to16(u8_t *buf24, fb_info jpeg_inf)
{
	u16_t *buf16 = NULL;
	
	u32_t r24,g24,b24;
	u32_t r16,g16,b16;
	u16_t rgb16;
	int i;

	if ((buf16 = malloc (jpeg_inf.w * jpeg_inf.h * 2)) == NULL)
		return NULL;

	for (i = 0 ;i < jpeg_inf.w * jpeg_inf.h ; i++)	{

		r24 = buf24[i * 3 + 0];
		g24 = buf24[i * 3 + 1];
		b24 = buf24[i * 3 + 2];	

		b16 = (b24 * 0x1f) / 0xff;
		g16 = (g24 * 0x3f) / 0xff;
		r16 = (r24 * 0x1f) / 0xff;
	
		rgb16 = b16 | (g16 <<5) | (r16 <<11);
	
		buf16[i] = rgb16;
	}
	return buf16;
}
#endif

#if 1
u8_t * scale24(u8_t *buf24, fb_info new_inf, fb_info jpeg_inf)
{
	int i,j;
	int dtw,dth;
	
	u8_t *buf = malloc(new_inf.w * new_inf.h * 3);

	if (buf == NULL) return NULL;
	
	for(j = 0; j < new_inf.h; j++){
		for(i = 0; i < new_inf.w; i++){

			dtw = i * jpeg_inf.w / new_inf.w;
			dth = j * jpeg_inf.h / new_inf.h;

			buf[0+i*3 + j*new_inf.w*3] = 
				buf24[0+(dtw+dth*jpeg_inf.w)*3];

			buf[1+i*3 + j*new_inf.w*3] = 
				buf24[1+(dtw+dth*jpeg_inf.w)*3];
			
			buf[2+i*3 + j*new_inf.w*3] =
				buf24[2+(dtw+dth*jpeg_inf.w)*3];
		}
	}

	return buf;
}
#endif
